<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>注册</title>
    <script th:src="@{/assets/js/core/jquery.min.js}"></script>
    <link th:href="@{/assets/css/sb-admin-2.min.css}" rel="stylesheet">

</head>

<body class="bg-gradient-primary">

<div class="container">

    <div class="card o-hidden border-0 shadow-lg my-5">
        <div class="card-body p-0">
            <!-- Nested Row within Card Body -->
            <div class="row">
                <div class="col-lg-5 d-none d-lg-block bg-register-image"></div>
                <div class="col-lg-7">
                    <div class="p-5">
                        <div class="text-center">
                            <h1 class="h4 text-gray-900 mb-4">创建账号!</h1>
                        </div>
                        <form class="user" id="register">
                            <div class="form-group">
                                <input type="password" class="form-control form-control-user" id="oldPassword" disabled
                                       name="oldPassword" placeholder="旧密码">
                            </div>
                            <div class="form-group">
                                <input type="password" class="form-control form-control-user" id="newPassword"
                                       name="newPassword" placeholder="新密码">
                                <span id="checkPsw"
                                      style="font-size: 20px; font-weight: bold;margin-right: 10px; margin-left:10px; color:#f3a199"></span>
                            </div>
                            <font color="red"></font>
                            <!--                   <button type="submit" class="btn btn-primary btn-user btn-block" id="registsubmit" onClick="javascript:regist()">注册</button>-->
                            <input type="button" class="btn btn-primary btn-user btn-block" id="registsubmit"
                                   onClick="javascript:regist();" value="获取机器码"/>
                            <hr>
                        </form>
                        <hr>
                        <div class="text-center">
                            <a class="small" th:href="@{/login}">已有账号? 登录!</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script th:replace="systemCommon/HarinCommon :: commonJs"></script>
<script type="text/javascript" th:inline="javascript">
    function checkPwd(pwdc) {
        var pwd = $("#regist_pwd").val();
        if ($(pwdc).val() == pwd) {
            $("#checkPsw").text("确认密码成功!!");
        } else {
            $("#checkPsw").css("color", "#f3461f");
            $("#checkPsw").text("两次密码不同!!");
        }
    }

    function checkUsername(username) {
        if (isEmpty($(username).val().trim())) {
            $("#checkMessage").text("用户名不能为空！！");
        } else {
            $("#checkMessage").text("");
            $(username).val($(username).val().trim());
            console.log($(username).val());
            $.ajax({
                type: "post",
                url: "/user/checkUsername",
                async: true,
                dataType: 'JSON',
                data: "username=" + $(username).val(),
                success: function (data) {
                    if (data.code == 200) {
                        //console.log(data.message);
                        $("#checkMessage").text(data.message);
                    } else if (data.code == 210) {
                        console.log(data.message);
                        $("#checkMessage").css("color", "#f3461f");
                        $("#checkMessage").text(data.message);
                    }
                }
            });
        }
    }

    function regist() {
        let flag = true;
        //所有属性转成数组
        var fields = $('#register').serializeArray();
        var obj = {};
        $.each(fields, function (index, field) {
            if (isEmpty(field.value)) {
                flag = false;
            }
            obj[field.name] = field.value;  //通过变量，将属性值，属性一起放到对象中
        });
        if (!flag) {
            alert("所有的信息必须都填写完，可以填写假数据。");
            return;
        }
        $.ajax({
            type: "post",
            url: "/user/register",
            async: true,
            contentType: 'application/json',
            data: JSON.stringify(obj),//将对象转为json字符串
            success: function (data) {
                if (data.code == 200) {
                    window.location.href = "/login";
                } else {
                    alert(data.message);
                }
            }
        });
    }

    //应该写在全局里
    function isEmpty(obj) {
        if (typeof obj == "undefined" || obj == null || obj == "") {
            return true;
        } else {
            return false;
        }
    }
</script>
</body>
</html>